package com.base.po.acess;

import java.util.Date;

import com.base.utils.GsonUtil;
import com.base.utils.Produce;

/**
 * 获取token的工具类
 * 
 * @author Administrator
 *
 */
public class TokenUtils {
	private final static String Key = "zjandzyc";

	/**
	 * 获取Token
	 * 
	 * @param parentid
	 * @param mobile
	 * @return
	 */
	public static String getToken(String parentid, String mobile) {
		if (parentid == null || parentid.equals("") || mobile == null || mobile.equals("")) {
			return null;
		}
		AcessToken acessToken = new AcessToken(parentid, mobile);
		return DesUtil.encrypt(GsonUtil.createJsonString(acessToken), DesUtil.CHARSET_GB2312, Key);
	}

	/**
	 * 
	 * @param code
	 * @return
	 */
	public static AcessToken getAcessToken(String tokenCode) {
		if (tokenCode == null) {
			return null;
		}
		try {
			AcessToken token = GsonUtil.getObject(DesUtil.decrypt(tokenCode, DesUtil.CHARSET_GB2312, Key),
					AcessToken.class);
			Date tokenDate = new Date(token.getCurrenttime());
			Date pushDate = Produce.plusDay(1, tokenDate);
			if (Produce.getNowDate().after(pushDate)) {
				token = null;
			}
			return token;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}
